package com.lineage.server.timecontroller.event;

import com.lineage.server.datatables.lock.ClanReading;
import com.lineage.server.model.L1Clan;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.world.WorldClan;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ClanSkillTimer extends TimerTask {
  private static final Log _log = LogFactory.getLog(ClanSkillTimer.class);
  
  private ScheduledFuture<?> _timer;
  
  public void start() {
    int timeMillis = 3600000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 3600000L, 3600000L);
  }
  
  public void run() {
    try {
      Collection allClan = WorldClan.get().getAllClans();
      if (allClan.isEmpty())
        return; 
      Iterator<L1Clan> iter = allClan.iterator();
      while (iter.hasNext()) {
        L1Clan clan = iter.next();
        if (clan.isClanskill()) {
          Timestamp skilltime = clan.get_skilltime();
          if (skilltime == null) {
            clan.set_clanskill(false);
            continue;
          } 
          Timestamp ts = new Timestamp(System.currentTimeMillis());
          if (skilltime.before(ts)) {
            clan.set_clanskill(false);
            clan.set_skilltime(null);
            ClanReading.get().updateClan(clan);
          } 
          Thread.sleep(1L);
        } 
      } 
    } catch (Exception e) {
      _log.error("血盟技能计时时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      ClanSkillTimer skillTimer = new ClanSkillTimer();
      skillTimer.start();
    } 
  }
}
